﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Mvc;
using WebWindow.Models;

namespace WebWindow.Controllers
{
    public class AcquiringController : Controller
    {
        //
        // GET: /Acquiring/

        public ActionResult Index()
        {
            ViewBag.PurchaseID = Request["PurchaseID"] ?? 0.ToString();
            ViewBag.PurchasePrice = Request["PurchasePrice"] ?? 0.ToString();
            ViewBag.Sign = Request["Sign"] ?? "";
            ViewBag.cb_url = Request["cb_url"] ?? "";
            ViewBag.success_url = Request["success_url"] ?? "";
            ViewBag.decline_url = Request["decline_url"] ?? "";
            return View();
        }

        public ActionResult Final()
        {
            var card = Request["card"];
            var cvv = Request["cvv"];
            var OrderID = Request["PurchaseID"];
            var OrderPrice = Request["PurchasePrice"];
            var Sign = Request["Sign"];
            var cb_url = Request["cb_url"];
            ViewBag.gobackUrl = Request["payResult"] == "success" ? Request["success_url"] : Request["decline_url"];
            var db = new SiteDataContext();
            db.acq_AcquiringEntry.InsertOnSubmit(new acq_AcquiringEntry{Card = card, cvv = cvv, PurchaseID = Convert.ToInt32(OrderID), PurchasePrice = Convert.ToDecimal(OrderPrice), Sign = Sign, TimeStamp = DateTime.Now});
            db.SubmitChanges();
            db.Dispose();

            //var request = WebRequest.Create(cb_url);
            //request.Method = "POST";
            //var encoding = new ASCIIEncoding();
            //var postData = "PurchaseID="+ OrderID;
            //var data = encoding.GetBytes(postData);
            //request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentLength = data.Length;
            //using (Stream newStream = request.GetRequestStream())
            //{
            //    newStream.Write(data, 0, data.Length);
            //}
            //request.GetResponse();

            return View();
        }

    }
}
